<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref } from "vue";
import {
  AddDangerSourceCommand,
  DangerSourcePageResponse,
  UpdateDangerSourceCommand,
  addDangerSourceApi,
  updateDangerSourceApi
} from "@/api/airport/dangerSource";
import { useUserStoreHook } from "@/store/modules/user";
import { ElMessage, FormInstance, FormRules } from "element-plus";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: DangerSourcePageResponse;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const formData = reactive<AddDangerSourceCommand | UpdateDangerSourceCommand>({
  actionMakeTime: "",
  correctiveAction: "",
  createTime: "",
  dangerSourceDescribe: "",
  emergeCause: "",
  precautionAction: "",
  precautionDeadline: "",
  relatedReformCode: "",
  relatedReformId: 0,
  riskCode: "",
  rootCause: "",
  dangerSourceId: 0,
  dangerSourceCode: "",
  dangerSourceName: "",
  dangerType: "",
  harmDescribe: "",
  correctiveDeadline: "",
  actionMakerName: ""
});

const rules: FormRules = {
  dangerSourceName: [
    { required: true, message: "危险源名称不能为空" }
  ],
  dangerSourceCode: [
    { required: true, message: "危险源编码不能为空" }
  ],
  harmDescribe: [
    { required: true, message: "危害描述不能为空" }
  ],
  correctiveDeadline: [
    { required: true, message: "整改期限不能为空" }
  ],
  actionMakerName: [
    { required: true, message: "责任人不能为空" }
  ]
};


const formRef = ref<FormInstance>();

function handleOpened() {
  if (props.row) {
    Object.assign(formData, props.row);
  } else {
    formRef.value?.resetFields();
  }
}

const loading = ref(false);

async function handleConfirm() {
  try {
    loading.value = true;
    if (props.type === "add") {
      await addDangerSourceApi(formData);
    } else if (props.type === "update") {
      await updateDangerSourceApi(formData as UpdateDangerSourceCommand);
    }
    ElMessage.info("提交成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "提交失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增危险源' : '更新危险源'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    @opened="handleOpened"
  >
    <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item prop="dangerSourceName" label="危险源名称" required>
        <el-input v-model="formData.dangerSourceName" />
      </el-form-item>
      <el-form-item prop="dangerSourceCode" label="危险源编码" required>
        <el-input v-model="formData.dangerSourceCode" />
      </el-form-item>
      <el-form-item prop="dangerType" label="危险类型">
        <el-input v-model="formData.dangerType" />
      </el-form-item>
      <el-form-item prop="harmDescribe" label="危害描述" required>
        <el-input v-model="formData.harmDescribe" type="textarea" rows="3" />
      </el-form-item>
      <el-form-item prop="correctiveDeadline" label="整改期限" required>
        <el-date-picker
          v-model="formData.correctiveDeadline"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item prop="actionMakerName" label="责任人" required>
        <el-input v-model="formData.actionMakerName" />
      </el-form-item>
    </el-form>
  </v-dialog>
</template>
